<?php


namespace app\controller;


use app\model\User;
use think\Exception;
use think\facade\Db;

class DataTest
{
public function index(){
    //$user = Db::table('tp_user')->select();
    //$user = Db::table('tp_user')->where('id',27)->find();
    //return Db::getLastSql();
    //$user = Db::table('tp_user')->where('id',27)->value('username');
    //dump($user);
    //$user = Db::name('user')->column('username','id');
    Db::name('user')->chunk(3,function ($users){
        foreach ($users as $user){
            dump($user);
        }
        echo 1;
    });
    //return json($user);
}
public function getUser(){
    $user = User::select();
    return json($user);
}
public function insert(){
    $aa = [
        "username"  => "徐林栋",
        "password"  => "123",
        "gender"    => "男",
        "email"     => "xulindong@163.com",
        "price"     => "70.00",
        "details"   => "123"
    ];
    return Db::name('user')->insert($aa);
}
public function getter(){
    //自动
//    Db::Transaction(function (){
//        Db::name('user')->where('id','19')->inc('price',3)->update();
//        Db::name('user1')->where('id','20')->dec('price',3)->update();
//    });

    //手动
    Db::startTrans();
    try {
        Db::name('user')->where('id','19')->inc('price',3)->update();
        Db::name('user1')->where('id','20')->dec('price',3)->update();

        Db::commit();
    }catch (\Exception $e){
        echo "执行SQL失败，开始回滚数据！";
        Db::rollback();
    }

}

}